﻿// CBitmapDlg.cpp: 实现文件
//

#include "pch.h"
#include "MFCReIn.h"
#include "afxdialogex.h"
#include "CBitmapDlg.h"


// CBitmapDlg 对话框

IMPLEMENT_DYNAMIC(CBitmapDlg, CDialogEx)

CBitmapDlg::CBitmapDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_BITMAP, pParent)
	, m_iBItsCount(64)
	, m_strParams(_T(""))
	, m_strResult(_T(""))
{
	hexData = (unsigned char*)malloc(8 * sizeof(unsigned char));
	memset(hexData, 0, 8);
}

CBitmapDlg::~CBitmapDlg()
{
}

void CBitmapDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	//DDX_Control(pDX, IDC_BITS, m_iBItsCount);
	DDX_Text(pDX, IDC_BITS, m_iBItsCount);
	DDV_MinMaxInt(pDX, m_iBItsCount, 1, 1240);
	DDX_Text(pDX, IDC_RESULTS, m_strParams);
	DDX_Text(pDX, IDC_RESULTS, m_strParams);
	(pDX, IDC_INDATA, m_strResult);
}


BEGIN_MESSAGE_MAP(CBitmapDlg, CDialogEx)
	ON_EN_CHANGE(IDC_INDATA, &CBitmapDlg::OnEnChangeIndata)
	ON_BN_CLICKED(IDC_BUTTON_GEN, &CBitmapDlg::OnBnClickedButtonGen)
	ON_BN_CLICKED(IDC_BUTTON_PARSE, &CBitmapDlg::OnBnClickedButtonParse)
	ON_BN_CLICKED(IDC_BUTTON_ONTOP, &CBitmapDlg::OnBnClickedButtonOntop)
	ON_BN_CLICKED(IDC_BUTTON_README, &CBitmapDlg::OnBnClickedButtonReadme)
END_MESSAGE_MAP()


// CBitmapDlg 消息处理程序


void CBitmapDlg::OnEnChangeIndata()
{
	// TODO:  如果该控件是 RICHEDIT 控件，它将不
	// 发送此通知，除非重写 CDialogEx::OnInitDialog()
	// 函数并调用 CRichEditCtrl().SetEventMask()，
	// 同时将 ENM_CHANGE 标志“或”运算到掩码中。

	// TODO:  在此添加控件通知处理程序代码
}


void CBitmapDlg::OnBnClickedButtonGen()
{
	// TODO: 在此添加控件通知处理程序代码
	int a[128];
	int i, j, k;
	CString tmp;
	TCHAR c;
	i = 0; j = 0; k = 0;
	UpdateData();
	for (i; i < m_strParams.GetLength(); i++)
	{
		c = m_strParams.GetAt(i);
		if ((c <= '9' && c >= '0') || c == ',')
		{
			if (c == ',')
				continue;
			for (j = i; j < m_strParams.GetLength(); j++)
			{
				if (m_strParams.GetAt(j) == ',')
					break;
			}
			tmp = m_strParams.Mid(i, j - i);
			sscanf(CT2A(tmp), "%d", &a[k]); k++; i = j;
		}
		else
		{
			break;
		}
	}
	memset(hexData, 0, m_iBItsCount / 8);
	for (i = 0; i < k; i++)
	{
		SetBit(a[i]);
	}
	tmp.Empty();
	m_strResult.Empty();
	for (i = 0; i < m_iBItsCount / 8; i++)
	{
		tmp.Format(_T("%02X"), hexData[i]);
		m_strResult += tmp;
	}
	UpdateData(FALSE);
}


void CBitmapDlg::OnBnClickedButtonParse()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData();
	memset(hexData, 0, m_iBItsCount / 8);
	int j = 0;
	for (int i = 0; i < m_strParams.GetLength(); i += 2)
	{
		sscanf(CT2A(m_strParams) + i, "%02X", &hexData[j]);
		j++;
	}
	CString tmp;
	m_strResult.Empty();
	for (i = 1; i <= m_iBItsCount; i++)
	{
		if (IsBitExsit(i))
		{
			tmp.Format(_T("%d,"), i);
			m_strResult += tmp;
		}
	}
	UpdateData(FALSE);
}
void CBitmapDlg::SetBit(int index)
{
	if (hexData == NULL || index > m_iBItsCount || index <= 0)
		return;
	unsigned char c = 0x80;
	int i = index - 1;
	hexData[i / 8] |= c >> (i % 8);
}

BOOL CBitmapDlg::IsBitExsit(int index)
{
	if (hexData == NULL)
		return FALSE;
	unsigned char c = 0x80;
	int i = index - 1;
	return ((hexData[i / 8]) & (c >> (i % 8))) != 0;
}

void CBitmapDlg::OnBnClickedButtonOntop()
{
	// TODO: 在此添加控件通知处理程序代码
	static BOOL bTop = FALSE;

	CRect rect;

	this->GetWindowRect(&rect);

	CWnd* pWnd;

	pWnd = this->GetDlgItem(IDC_BUTTON_ONTOP);
	bTop = !bTop;

	if (bTop) {
		this->SetWindowPos(&wndTopMost, rect.left, rect.top, rect.Width(), rect.Height(), WS_EX_TOPMOST);
		pWnd->SetWindowText(_T("top"));
	}
	else {
		this->SetWindowPos(&wndBottom, rect.left, rect.top, rect.Width(), rect.Height(), SWP_SHOWWINDOW);
		pWnd->SetWindowText(_T("back"));
	}
}


void CBitmapDlg::OnBnClickedButtonReadme()
{
	// TODO: 在此添加控件通知处理程序代码
}
